﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace VS.Commutil
{
    public class WebUtil
    {
        #region 跟据当前时间加上四位随机数生成一个文件名
        public static string Make_FileName()
        {
            Random random1 = new Random();
            string text1 = DateTime.Now.DayOfYear.ToString() + DateTime.Now.Minute.ToString();
            text1 = text1.Replace("-", "").Replace(":", "").Replace("PM", "").Replace("AM", "").Replace("\u4e0a\u5348", "").Replace("\u4e0b\u5348", "").Replace(" ", "");
            int num1 = random1.Next(0x3e8, 0x270f);
            return (text1 + num1.ToString());
            //return Guid.NewGuid().ToString();
        }
        #endregion

        #region 新建一个空文件夹
        public static bool Create_Directory(string path)
        {
            bool flag1 = false;
            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                flag1 = true;
            }
            catch (Exception)
            {
                flag1 = false;
            }
            return flag1;
        }
        #endregion

        #region 删除一个文件
        public static bool Delete_File(string path)
        {
            bool flag1 = false;
            try
            {
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                flag1 = true;
            }
            catch (Exception)
            {
                flag1 = false;
            }
            return flag1;
        }
        #endregion

        #region 将一个数字日期转换成文件名，前面不足的补零 FormatDateTime2FileNameString
        public static string FormatDateTime2FileNameString()
        {
            string s;
            DateTime thedate = DateTime.Now;
            s = FormatNumber2String(thedate.Year, 4) + FormatNumber2String(thedate.Month, 2) + FormatNumber2String(thedate.Day, 2) + FormatNumber2String(thedate.Hour, 2) + FormatNumber2String(thedate.Minute, 2) + FormatNumber2String(thedate.Second, 2);
            return s;
        }

        public static string FormatDateTime2FileNameString(System.DateTime SelectedDate)
        {
            string s;
            DateTime thedate = SelectedDate;
            s = FormatNumber2String(thedate.Year, 4) + FormatNumber2String(thedate.Month, 2) + FormatNumber2String(thedate.Day, 2) + FormatNumber2String(thedate.Hour, 2) + FormatNumber2String(thedate.Minute, 2) + FormatNumber2String(thedate.Second, 2);
            return s;
        }

        public static string FormatDate2DirectoryName()
        {
            string s;
            DateTime thedate = DateTime.Now;
            s = FormatNumber2String(thedate.Year, 4) + FormatNumber2String(thedate.Month, 2) + FormatNumber2String(thedate.Day, 2);
            return s;
        }

        public static string FormatDate2DirectoryName(System.DateTime SelectedDate)
        {
            string s;
            DateTime thedate = SelectedDate;
            s = FormatNumber2String(thedate.Year, 4) + FormatNumber2String(thedate.Month, 2) + FormatNumber2String(thedate.Day, 2);
            return s;
        }
        #endregion

        #region 将一个数字日期转换成标准字符串，前面不足的补零 FormatDate2String
        public static string FormatDate2String(System.DateTime theDate)
        {
            string s;
            s = FormatNumber2String(theDate.Year, 4) + "-" + FormatNumber2String(theDate.Month, 2) + "-" + FormatNumber2String(theDate.Day, 2);
            return s;
        }
        #endregion

        #region 将一个数字转换成指定长度的字符串，前面不足的补零 FormatNumber2String
        public static string FormatNumber2String(long intNumber, Int16 intLength)
        {
            string s;
            s = intNumber.ToString();
            while (s.Length < intLength)
            {
                s = "0" + s;
            }
            return s;
        }
        #endregion

        #region MD5加密
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static string MD5(string number)
        {
            ASCIIEncoding ASCIIenc = new ASCIIEncoding();
            string strReturn = "";
            byte[] ByteSourceText = ASCIIenc.GetBytes(number);
            MD5CryptoServiceProvider Md5Hash = new MD5CryptoServiceProvider();
            byte[] ByteHash = Md5Hash.ComputeHash(ByteSourceText);
            foreach (byte b in ByteHash)
            {
                strReturn += b.ToString("x2");
            }
            return strReturn;
        }
        #endregion
    }
}